//
//  HWISOMapper.m
//  HOLLA
//
//  Created by Xu Zhong on 2018/11/13.
//  Copyright © 2018 HOLLA. All rights reserved.
//

#import "XCSMSISOMapper.h"
#import "Config.h"
#import "XCSMSTheme.h"

@implementation XCSMSISOCountry

@end

@interface XCSMSISOMapper ()

@property (nonatomic, strong) NSMutableArray<XCSMSISOCountry *> *list;

@end

@implementation XCSMSISOMapper

+ (instancetype)sharedMapper {
	static dispatch_once_t onceToken;
	static XCSMSISOMapper *mapper = nil;
	dispatch_once(&onceToken, ^{
		mapper = [[self alloc] init];
	});
	return mapper;
}

- (instancetype)init {
	if (self = [super init]) {
        self.list = [NSMutableArray arrayWithCapacity:0];
	}
	return self;
}

+ (NSArray<XCSMSISOCountry *> *)countryList {
    XCSMSISOMapper *mapper = [XCSMSISOMapper sharedMapper];
    [mapper readDataFromJSONFile];
    return mapper.list;
}

- (void)readDataFromJSONFile {
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"CAMPROSMSKit" ofType:@"bundle"]];
    NSString *path = [bundle pathForResource:@"campro_json/campro_country_list" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    
    [self.list removeAllObjects];
    NSString *language = [XCSMSISOMapper hw_LanguageCode];
    for (NSDictionary *dic in json) {
        XCSMSISOCountry *country = [[XCSMSISOCountry alloc] init];
        country.countryName = [dic objectForKey:language];
        if ([country.countryName length] == 0) {
            country.countryName = [dic objectForKey:@"en"];
        }
        
        country.countryCode = [NSString stringWithFormat:@"+%@", [dic objectForKey:@"code"]];
        country.locale = [dic objectForKey:@"locale"];
        country.countryFlag = [XCSMSISOMapper hw_codeToFlag:country.locale];
        country.initial = [dic objectForKey:@"initial"];
        [self.list addObject:country];
    }
}

+ (NSString *)hw_LanguageCode {
    if (SupportMultiLanguages()) {
        NSString *language = [[NSLocale preferredLanguages] firstObject];
        
        if ([language hasPrefix:@"en"])            return @"en";    //english
        if ([language hasPrefix:@"fr"])            return @"fr";    //french
        if ([language hasPrefix:@"es"])            return @"es";    //spanish
        if ([language hasPrefix:@"pt"])            return @"pt";    //portuguese
        if ([language hasPrefix:@"it"])            return @"it";    //italian
        if ([language hasPrefix:@"de"])            return @"de";    //german
        if ([language hasPrefix:@"ja"])            return @"ja";    //japanese
        if ([language hasPrefix:@"ko"])            return @"ko";    //korean
        if ([language hasPrefix:@"vi"])            return @"vi";    //vietnamese
        if ([language hasPrefix:@"ru"])            return @"ru";    //russian
        if ([language hasPrefix:@"tr"])            return @"tr";    //turkish
        if ([language hasPrefix:@"id"])            return @"id";    //indonesian
        if ([language hasPrefix:@"th"])            return @"th";    //thailand
        if ([language hasPrefix:@"ar"])            return @"ar";    //arabic
        if ([language hasPrefix:@"hi"])            return @"hi";    //hindi
        if ([language hasPrefix:@"ro"])            return @"ro";    //romanian
        if ([language hasPrefix:@"zh-Hant"])       return @"zh-rTW";
        if ([language hasPrefix:@"zh-Hans"])       return @"zh";
        if ([language hasPrefix:@"zh-HK"])         return @"zh-rTW";
        if ([language hasPrefix:@"zh-TW"])         return @"zh-rTW";
        if ([language hasPrefix:@"zh"])            return @"zh";    //chinese
    }
    
    return @"en";
}

+ (NSString *)hw_codeToFlag:(NSString *)countryCode {
    int base = 127397;
    
    wchar_t bytes[2] = {
        base + [countryCode characterAtIndex:0],
        base + [countryCode characterAtIndex:1]
    };
    
    return [[NSString alloc] initWithBytes:bytes length:[countryCode length] * sizeof(wchar_t) encoding:NSUTF32LittleEndianStringEncoding];
}

@end
